package com.example.pkmedical.Fragments;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.pkmedical.APP.MyApp;
import com.example.pkmedical.Activity.rollovershock.RolloverShockCantStartActivity;
import com.example.pkmedical.Base.BaseFragment;
import com.example.pkmedical.Event.MessageWrap;
import com.example.pkmedical.R;
import com.example.pkmedical.Utils.CommandUtil;
import com.example.pkmedical.Utils.DistinguishUtil;
import com.example.pkmedical.Utils.IoTimer;
import com.example.pkmedical.Utils.SerialProtUtil;
import com.example.pkmedical.Utils.TestTime;
import com.example.pkmedical.Utils.TimeUtils;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public class RoutineFragment extends BaseFragment {
    @BindView(R.id.fm_cg_cgimg)
    ImageView fmCgCgimg;
    @BindView(R.id.fm_cg_cgtext)
    TextView fmCgCgtext;
    @BindView(R.id.fm_cg_cgms)
    RelativeLayout fmCgCgms;
    @BindView(R.id.fm_cg_smms)
    RelativeLayout fmCgSmms;
    @BindView(R.id.fm_cg_zdcq)
    RelativeLayout fmCgZdcq;
    @BindView(R.id.fm_cg_tbfq)
    RelativeLayout fmCgTbfq;
    @BindView(R.id.fm_cg_ssms)
    RelativeLayout fmCgSsms;
    @BindView(R.id.fm_cg_zfms)
    RelativeLayout fmCgZfms;
    @BindView(R.id.fm_cg_yfms)
    RelativeLayout fmCgYfms;
    @BindView(R.id.fm_cg_restms)
    RelativeLayout fmCgRestms;
    @BindView(R.id.fm_cg_smimg)
    ImageView fmCgSmimg;
    @BindView(R.id.fm_cg_smtext)
    TextView fmCgSmtext;
    @BindView(R.id.fm_cg_zdimg)
    ImageView fmCgZdimg;
    @BindView(R.id.fm_cg_zdtext)
    TextView fmCgZdtext;
    @BindView(R.id.fm_cg_tbfqimg)
    ImageView fmCgTbfqimg;
    @BindView(R.id.fm_cg_tbfqtext)
    TextView fmCgTbfqtext;
    @BindView(R.id.fm_cg_ssmsimg)
    ImageView fmCgSsmsimg;
    @BindView(R.id.fm_cg_ssmstext)
    TextView fmCgSsmstext;
    @BindView(R.id.fm_cg_zfmsimg)
    ImageView fmCgZfmsimg;
    @BindView(R.id.fm_cg_zfmstext)
    TextView fmCgZfmstext;
    @BindView(R.id.fm_cg_yfmsimg)
    ImageView fmCgYfmsimg;
    @BindView(R.id.fm_cg_yfmstext)
    TextView fmCgYfmstext;
    @BindView(R.id.fm_cg_restimg)
    ImageView fmCgRestimg;
    @BindView(R.id.fm_cg_resttext)
    TextView fmCgResttext;
    boolean HelpType = false;
    boolean CgType = false;
    boolean SsType = false;
    boolean SmType = false;
    boolean ZfType = false;
    boolean ZdType60 = false;
    boolean YfType = false;
    boolean PgType = false;
    boolean ZdType30 = false;
    private static final String TAG = "yangshuo";
    @BindView(R.id.fm_cg_title_text)
    TextView fmCgTitleText;
    @BindView(R.id.fm_cg_update_progress)
    ProgressBar fmCgUpdateProgress;
    @BindView(R.id.fm_cg_bottom_time)
    TextView fmCgBottomTime;
    private byte[] mBytes;
    private int mRestType = 1;
    private boolean isRestRunning = false;
    private int RestNum = 0;
    private boolean isResponseOk = true;
    private boolean IS_ZDCQ = false;
    private boolean IS_HELP = false;
    private boolean IS_JUST = true;

    @Override
    public int setLayoutId() {
        return R.layout.fragment_changguii;
    }

    @Override
    public void findViewById(View view) {

    }

    @Override
    protected boolean isRegisteredEventBus() {
        return true;
    }

    @Override
    public void onDestroy() {
        if (mTimeCountDisposable != null) {
            mTimeCountDisposable.dispose();
        }
        super.onDestroy();

    }

    @Override
    public void onReceiveEvent(MessageWrap event) {
        super.onReceiveEvent(event);
        if (event.getCode() == MessageWrap.QIDIANMOSHI) {
            Log.d(TAG, "onDataReceivedChangGuiFragment [ byte[] ]: " + Arrays.toString(event.getBytes()));
            mBytes = event.getBytes();
            distinguish(mBytes);
        } else if (event.getCode() == MessageWrap.ROLLOVERIMG) {
            mBytes = event.getBytes();
            if (mBytes[3] == 11 && mBytes[4] == 1) {
                mActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
//                        SerialProtUtil.getInstance().sendBytes50(2, CommandUtil.CHANGGUI_TTYPE);
//                        SerialProtUtil.getInstance().sendBytes(2, CommandUtil.OUT_ZDCQ);
//                        SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.OUT_HELP);
                    }
                });
                return;
            }
        } else if (event.getCode() == MessageWrap.USER_MESSAGE_TIME) {
            mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    allFalse();
                    allOff();
                }
            });
        } else if (event.getCode() == MessageWrap.STOP_TIMER) {
            Log.d("yangshuoSYOP", "***********");
            if (mTimeCountDisposableOPT != null) {
                mTimeCountDisposableOPT.dispose();
            }
        }
    }

    @Override
    public void setViewData(View view) {

    }

    @Override
    public void setClickEvent(View view) {
    }

    /**
     * 返回数据识别
     */
    private void distinguish(byte[] bytes) {
        ZdType30 = false;
        if (!ZdType60) {
            mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (bytes[3] != 0x08) {
                        if (mTimeCountDisposableOPT != null) {
                            mTimeCountDisposableOPT.dispose();
                        }
                        isRestRunning = false;
                        allFalse();
                        allOff();
                    } else {
                        allFalse2();
                        allOff();
                    }
                }
            });
        } else {
            mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (bytes[3] != 0x08) {
                        if (mTimeCountDisposableOPT != null) {
                            mTimeCountDisposableOPT.dispose();
                        }
                        isRestRunning = false;
                    }
                    allFalse();
                    allOff();


                }
            });
        }
        if (DistinguishUtil.distinguishUtil(bytes)) {
            mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (mTimeCountDisposable != null && !isRestRunning && !ZdType60) {
                        mTimeCountDisposable.dispose();
                    }
                    CommandUtil.IS_LEFT_RIGHT_ALL = false;
                    CommandUtil.IS_LEFT_RIGHT_ANOTHER = false;
                    switch (bytes[3]) {
                        case 0x01:
                            ZdType60 = false;
                            IS_HELP = false;
                            IS_ZDCQ = false;
                            fmCgTitleText.setText(R.string.routine_cgms);
                            CgType = true;
                            cgms_on();
                            isRestRunning = false;
                            RestNum = 0;
                            TimeCount();
                            break;
                        case 0x02:
                            ZdType60 = false;
                            IS_HELP = false;
                            IS_ZDCQ = false;
                            fmCgTitleText.setText(R.string.routine_smms);
                            SmType = true;
                            smms_on();
                            isRestRunning = false;
                            RestNum = 0;
                            TimeCount();
                            break;
                        case 0x03:
                            IS_HELP = false;
                            IS_ZDCQ = true;
                            fmCgTitleText.setText(R.string.routine_zdcq);
                            ZdType30 = true;
                            ZdType60 = false;
                            zdcq_on();
                            isRestRunning = false;
                            RestNum = 0;
                            TimeCount();

                            break;
                        case 0x04:
                            ZdType60 = false;
                            IS_HELP = false;
                            IS_ZDCQ = false;
                            fmCgTitleText.setText(R.string.routine_tbfq);
                            PgType = true;
                            tbfq_on();
                            isRestRunning = false;
                            RestNum = 0;
                            TimeCount();

                            break;
                        case 0x05:
                            CommandUtil.IS_LEFT_RIGHT_ALL= true;
                            CommandUtil.IS_LEFT_RIGHT_ANOTHER = true;
                            ZdType60 = false;
                            IS_HELP = false;
                            IS_ZDCQ = false;
                            fmCgTitleText.setText(R.string.routine_zfms);
                            ZfType = true;
                            zfms_on();
                            isRestRunning = false;
                            RestNum = 0;
                            TimeCount();
                            break;
                        case 0x06:
                            CommandUtil.IS_LEFT_RIGHT_ALL= true;
                            CommandUtil.IS_LEFT_RIGHT_ANOTHER = true;
                            ZdType60 = false;
                            IS_HELP = false;
                            IS_ZDCQ = false;
                            fmCgTitleText.setText(R.string.routine_yfms);
                            YfType = true;
                            yfms_on();
                            isRestRunning = false;
                            RestNum = 0;
                            TimeCount();
                            break;
                        case 0x07:
                            ZdType60 = false;
                            IS_HELP = false;
                            IS_ZDCQ = false;
                            fmCgTitleText.setText(R.string.routine_ssms);
                            SsType = true;
                            ssms_on();
                            isRestRunning = false;
                            RestNum = 0;
                            TimeCount();

                            break;
                        case 0x08:
                            ZdType60 = false;
                            IS_HELP = false;
                            IS_ZDCQ = false;
                            fmCgTitleText.setText(R.string.routine_rest);
                            rest_on();
                            isRestRunning = true;
                            RestNum++;
                            if (RestNum == 1) {
                                TimeCount();
                            }
                            break;
                        case 0x09://护栏最大充气
                            ZdType60 = false;
                            IS_HELP = false;
                            IS_ZDCQ = true;
                            if (!ZdType30) {
                                //最大充气
                                SerialProtUtil.getInstance().sendBytes(2, CommandUtil.ZUIDACHONGQI_TTYPE);
                                SerialProtUtil.getInstance().sendBytes300(2, CommandUtil.IN_ZDCQ);
                                isResponseOk = false;
                            }
                            break;
                        case 0x0a://护栏常规
                            ZdType60 = false;
                            if (!CgType) {
                                //常规模式
                                SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.CHANGGUI_TTYPE);
                                isResponseOk = false;
                            }
                            break;
                        case 0x0b://侧出口帮助床体接收
                            if (!HelpType) {
                                //侧出口帮助模式
                                SerialProtUtil.getInstance().sendBytes(2, CommandUtil.HELP_BED);
                                SerialProtUtil.getInstance().sendBytes300(2, CommandUtil.OUT_ZDCQ);
                                isResponseOk = false;
                            }

                            break;
                        case 0x0c://侧出口帮助气垫接收
                            ZdType60 = false;
                            IS_HELP = true;
                            IS_ZDCQ = false;
                            fmCgTitleText.setText(R.string.routine_help);
                            HelpType = true;
                            isRestRunning = false;
                            RestNum = 0;
                            TimeCount();
                            break;
                        case 0x0d://最大充气60分
                            IS_HELP = false;
                            IS_ZDCQ = true;
//                            if (!ZdType60) {
                            //最大充气
                            SerialProtUtil.getInstance().sendBytes(2, CommandUtil.ZUIDACHONGQI_TTYPE_HOUR);
                            SerialProtUtil.getInstance().sendBytes300(2, CommandUtil.IN_ZDCQ);
                            isResponseOk = false;
//                            }
                            break;
                        case 0x0e://最大充气一小时气垫接收
                            IS_HELP = false;
                            IS_ZDCQ = true;
                            ZdType30 = false;
                            fmCgTitleText.setText(R.string.routine_zdcq);
                            ZdType60 = true;
                            zdcq_on();
                            isRestRunning = false;
                            RestNum = 0;
                            TimeCount();
                            break;
                    }
                    isResponseOk = true;
                }
            });

        }
    }

    @OnClick({R.id.fm_cg_cgms, R.id.fm_cg_smms, R.id.fm_cg_zdcq, R.id.fm_cg_tbfq, R.id.fm_cg_ssms, R.id.fm_cg_zfms, R.id.fm_cg_yfms, R.id.fm_cg_restms})
    public void onViewClicked(View view) {
//        ZdType = false;
        switch (view.getId()) {
            //常规模式0x01
            case R.id.fm_cg_cgms:
//                if (isResponseOk) {
                if (!CgType) {
                    changeNowType(false);
                    //常规模式
                    SerialProtUtil.getInstance().sendBytes300(2, CommandUtil.CHANGGUI_TTYPE);
                    isResponseOk = false;
                }
//                }
                break;
            case R.id.fm_cg_smms:
                Log.d("isResponseOk", isResponseOk + "shuimian");
                if (isResponseOk) {
                    changeNowType(false);
                    if (!SmType) {
                        //睡眠模式
                        SerialProtUtil.getInstance().sendBytes300(2, CommandUtil.SELLP_TTYPE);
                        isResponseOk = false;
                    }
                }

                break;
            case R.id.fm_cg_zdcq:
                Log.d("isResponseOk", isResponseOk + "zuida");
                if (isResponseOk) {
                    if (!ZdType30) {
                        //最大充气
                        changeNowType(true);
                        SerialProtUtil.getInstance().sendBytes300(2, CommandUtil.ZUIDACHONGQI_TTYPE);
                        isResponseOk = false;
                    }
                }

                break;
            case R.id.fm_cg_tbfq:
                Log.d("isResponseOk", isResponseOk + "tunbufangqi");
                if (isResponseOk) {
                    if (!PgType) {
                        changeNowType(false);
                        //臀部放气
                        SerialProtUtil.getInstance().sendBytes300(2, CommandUtil.TUNBUFANGQI_TTYPE);
                        isResponseOk = false;
                    }
                }

                break;
            case R.id.fm_cg_ssms:
                allFalse();
                allOff();
                if (!SsType) {
                    changeNowType(false);
                    fmCgTitleText.setText(R.string.routine_ssms);
                    SsType = true;
                    ssms_on();
                    isRestRunning = false;
                    RestNum = 0;
                    TimeCount();
                }
                if (isRestRunning) {
                    if (mTimeCountDisposableOPT != null) {
                        mTimeCountDisposableOPT.dispose();
                    }
                    isRestRunning = false;
                }
                SsType = true;
                startActivity(new Intent(MyApp.getAppContext(), ComfortableActivity.class));

                break;
            case R.id.fm_cg_zfms:
                Log.d("yangshuobbbbbb", CommandUtil.BED_FRONT + "-----" + CommandUtil.BED_AFTER + "");
                if (CommandUtil.RALL_ONE && CommandUtil.RALL_TWO && CommandUtil.RALL_THREE && CommandUtil.RALL_FOUR && CommandUtil.BED_FRONT < 40 && CommandUtil.BED_AFTER < 25) {
                    if (isResponseOk) {
                        changeNowType(false);
                        if (!ZfType) {
                            //左翻模式
                            SerialProtUtil.getInstance().sendBytes300(2, CommandUtil.ZUOFAN_TTYPE);
                            isResponseOk = false;
                        }
                    }
                } else {
                    startActivity(new Intent(MyApp.getAppContext(), RolloverShockCantStartActivity.class));
                }


                break;
            case R.id.fm_cg_yfms:
                Log.d("yangshuobbbbbb", CommandUtil.BED_FRONT + CommandUtil.BED_AFTER + "");
                if (CommandUtil.RALL_ONE && CommandUtil.RALL_TWO && CommandUtil.RALL_THREE && CommandUtil.RALL_FOUR && CommandUtil.BED_FRONT < 40 && CommandUtil.BED_AFTER < 25) {
                    if (isResponseOk) {
                        changeNowType(false);
                        if (!YfType) {
                            //右翻
                            SerialProtUtil.getInstance().sendBytes300(2, CommandUtil.YOUFAN_TTYPE);
                            isResponseOk = false;
                        }
                    }
                } else {
                    startActivity(new Intent(MyApp.getAppContext(), RolloverShockCantStartActivity.class));
                }


                break;
            case R.id.fm_cg_restms:
                Log.d("isResponseOk", isResponseOk + "rest");
                if (isResponseOk) {
                    if (!isRestRunning) {
                        changeNowType(false);
                        isRestRunning = true;
                        RestNum = 0;
                        isResponseOk = false;
                        TimeCountOPT();
                    }

                }

                break;

        }

    }

    private void allOff() {
        cgms_off();
        smms_off();
        ssms_off();
        zfms_off();
        yfms_off();
        zdcq_off();
        tbfq_off();
        rest_off();
    }

    private void allFalse() {
        HelpType = false;
        CgType = false;
        SsType = false;
        SmType = false;
        ZfType = false;
//        ZdType = false;
        YfType = false;
        PgType = false;
        isRestRunning = false;
    }

    private void allFalse2() {
        HelpType = false;
        CgType = false;
        SsType = false;
        SmType = false;
        ZfType = false;
//        ZdType = false;
        YfType = false;
        PgType = false;
        isRestRunning = true;
    }

    private void cgms_on() {
        fmCgCgms.setBackgroundResource(R.drawable.qdms_item_back_on);
        fmCgCgimg.setImageResource(R.drawable.qdms_cgms_on);
        fmCgCgtext.setTextColor(getResources().getColor(R.color.green_back_text));
    }

    private void cgms_off() {
        fmCgCgms.setBackgroundResource(R.drawable.changgui_item_back_off);
        fmCgCgimg.setImageResource(R.drawable.img_changgui_cgms_off);
        fmCgCgtext.setTextColor(getResources().getColor(R.color.white));
    }

    private void smms_on() {
        fmCgSmms.setBackgroundResource(R.drawable.changgui_item_back_on);
        fmCgSmimg.setImageResource(R.drawable.img_changgui_smms_on);
        fmCgSmtext.setTextColor(getResources().getColor(R.color.green_back_text));
    }

    private void smms_off() {
        fmCgSmms.setBackgroundResource(R.drawable.changgui_item_back_off);
        fmCgSmimg.setImageResource(R.drawable.img_changgui_smms_off);
        fmCgSmtext.setTextColor(getResources().getColor(R.color.white));
    }

    private void zdcq_on() {
        fmCgZdcq.setBackgroundResource(R.drawable.qdms_item_back_on);
        fmCgZdimg.setImageResource(R.drawable.img_changgui_zdcq_on);
        fmCgZdtext.setTextColor(getResources().getColor(R.color.green_back_text));
    }

    private void zdcq_off() {
        fmCgZdcq.setBackgroundResource(R.drawable.changgui_item_back_off);
        fmCgZdimg.setImageResource(R.drawable.img_changgui_zdcq_off);
        fmCgZdtext.setTextColor(getResources().getColor(R.color.white));
    }

    private void tbfq_on() {
        fmCgTbfq.setBackgroundResource(R.drawable.qdms_item_back_on);
        fmCgTbfqimg.setImageResource(R.drawable.img_changgui_tbfq_on);
        fmCgTbfqtext.setTextColor(getResources().getColor(R.color.green_back_text));
    }

    private void tbfq_off() {
        fmCgTbfq.setBackgroundResource(R.drawable.changgui_item_back_off);
        fmCgTbfqimg.setImageResource(R.drawable.qdms_dbfq_off);
        fmCgTbfqtext.setTextColor(getResources().getColor(R.color.white));
    }

    private void ssms_on() {
        fmCgSsms.setBackgroundResource(R.drawable.qdms_item_back_on);
        fmCgSsmsimg.setImageResource(R.drawable.img_changgui_ssms_on);
        fmCgSsmstext.setTextColor(getResources().getColor(R.color.green_back_text));
    }

    private void ssms_off() {
        fmCgSsms.setBackgroundResource(R.drawable.changgui_item_back_off);
        fmCgSsmsimg.setImageResource(R.drawable.img_changgui_ssms_off);
        fmCgSsmstext.setTextColor(getResources().getColor(R.color.white));
    }

    private void zfms_on() {
        fmCgZfms.setBackgroundResource(R.drawable.qdms_item_back_on);
        fmCgZfmsimg.setImageResource(R.drawable.img_changgui_zuofan_on);
        fmCgZfmstext.setTextColor(getResources().getColor(R.color.green_back_text));
    }

    private void zfms_off() {
        fmCgZfms.setBackgroundResource(R.drawable.changgui_item_back_off);
        fmCgZfmsimg.setImageResource(R.drawable.img_changgui_zuofan_off);
        fmCgZfmstext.setTextColor(getResources().getColor(R.color.white));
    }

    private void yfms_on() {
        fmCgYfms.setBackgroundResource(R.drawable.qdms_item_back_on);
        fmCgYfmsimg.setImageResource(R.drawable.img_changgui_youfan_on);
        fmCgYfmstext.setTextColor(getResources().getColor(R.color.green_back_text));
    }

    private void yfms_off() {
        fmCgYfms.setBackgroundResource(R.drawable.changgui_item_back_off);
        fmCgYfmsimg.setImageResource(R.drawable.img_changgui_youfan_off);
        fmCgYfmstext.setTextColor(getResources().getColor(R.color.white));
    }

    private void rest_on() {
        fmCgRestms.setBackgroundResource(R.drawable.qdms_item_back_on);
        fmCgRestimg.setImageResource(R.drawable.img_changgui_opt_on);
        fmCgResttext.setTextColor(getResources().getColor(R.color.green_back_text));
    }

    private void rest_off() {
        fmCgRestms.setBackgroundResource(R.drawable.changgui_item_back_off);
        fmCgRestimg.setImageResource(R.drawable.img_changgui_opt_off);
        fmCgResttext.setTextColor(getResources().getColor(R.color.white));
    }

    private int nowProgress = 0;
    private int timeLong = 1800000;
    private int CgZero = 0;
    private Disposable mTimeCountDisposable;


    /**
     * 倒计时
     */
    public void TimeCount() {
        CgZero = 0;
        if (CgType) {//常规模式时间24小时
            IS_JUST = true;
            timeLong = 8640000;
//            fmCgUpdateProgress.setMax(86400);
            fmCgUpdateProgress.setVisibility(View.GONE);
        } else if (SmType) {//睡眠模式时间8小时
            IS_JUST = false;
            timeLong = 28800;
            fmCgUpdateProgress.setMax(28800);
            fmCgUpdateProgress.setVisibility(View.VISIBLE);
        } else if (ZfType || ZdType60 || ZdType30 || YfType || PgType || HelpType) {//最大充气模式、左翻、右翻时间0.5小时
            IS_JUST = false;
            if (ZdType60) {
                timeLong = 3600;
                fmCgUpdateProgress.setMax(3600);
            } else {
                timeLong = 1800;
                fmCgUpdateProgress.setMax(1800);
            }
            fmCgUpdateProgress.setVisibility(View.VISIBLE);
        } else {
            IS_JUST = true;
            fmCgUpdateProgress.setVisibility(View.GONE);
            timeLong = 8640000;
//            fmCgUpdateProgress.setMax(86400);

        }
        // 是否为正计时
        if (IS_JUST) {
            fmCgBottomTime.setText(TimeUtils.generateTime(CgZero));
        } else {
            fmCgBottomTime.setText(TimeUtils.generateTime(timeLong));
        }
        nowProgress = timeLong;
        fmCgUpdateProgress.setProgress(0);
        if (mTimeCountDisposable != null) {
            //取消上一次的订阅
            mTimeCountDisposable.dispose();
        }
//        //0-count依次输出，延时0s执行，每1s发射一次。
        mTimeCountDisposable = Flowable.intervalRange(0, timeLong, 0, 1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(aLong -> {
                    if (IS_JUST) {
                        fmCgBottomTime.setText(TimeUtils.generateTime(CgZero += 1));
                    } else {
                        fmCgBottomTime.setText(TimeUtils.generateTime(timeLong -= 1));
                        fmCgUpdateProgress.setProgress(nowProgress -= 1);
                    }

                })
                .doOnComplete(() -> {
                    if (mTimeCountDisposable != null) {
                        mTimeCountDisposable.dispose();
                    }
                    if (!CgType) {
                        if (IS_ZDCQ) {
                            SerialProtUtil.getInstance().sendBytes(2, CommandUtil.OUT_ZDCQ);
                        } else if (IS_HELP) {
                            SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.OUT_HELP);
                        }
                        SerialProtUtil.getInstance().sendBytes300(2, CommandUtil.CHANGGUI_TTYPE);
                    }
                })
                .subscribe();
    }

    private void changeNowType(boolean isZDCQ) {
        if (!isZDCQ) {
            if (IS_ZDCQ) {
                SerialProtUtil.getInstance().sendBytes500(2, CommandUtil.OUT_ZDCQ);
            }
            if (IS_HELP) {
                SerialProtUtil.getInstance().sendBytes400(2, CommandUtil.OUT_HELP);
            }
        } else {
            SerialProtUtil.getInstance().sendBytes500(2, CommandUtil.IN_ZDCQ);
            if (IS_HELP) {
                SerialProtUtil.getInstance().sendBytes400(2, CommandUtil.OUT_HELP);
            }
        }
    }

    private Disposable mTimeCountDisposableOPT;

    /**
     * 倒计时
     */
    public void TimeCountOPT() {
        mTimeCountDisposableOPT = Flowable.intervalRange(0, 999999999, 0, 30, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(aLong -> {
                    switch (mRestType) {
                        case 1:
                            SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.REST_ONE);
                            mRestType = 2;
                            break;
                        case 2:
                            SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.REST_TWO);
                            mRestType = 3;
                            break;
                        case 3:
                            SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.REST_THREE);
                            mRestType = 4;
                            break;
                        case 4:
                            SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.REST_FOUR);
                            mRestType = 1;
                            break;
                    }
                })
                .doOnComplete(() -> {
                    if (mTimeCountDisposableOPT != null) {
                        mTimeCountDisposableOPT.dispose();
                    }
                    if (!CgType) {
                        SerialProtUtil.getInstance().sendBytes(2, CommandUtil.CHANGGUI_TTYPE);
                    }
                })
                .subscribe();
    }
}